﻿Public Class F8_Phieu_Chi

    Dim DS As New List(Of PhieuChi)

    Sub HienThi()
        DS = XL_PhieuChi.LayDS(-1, -1, -1, -1, -1, Me.Tag, cbx_thang.SelectedIndex, cbx_nam.SelectedItem, Date.MinValue)
        lsvphieuchi.Items.Clear()
        For Each p In DS
            Dim li As ListViewItem = New ListViewItem(p.Ngay)
            li.Tag = p
            li.SubItems.Add(p.SoTien)
            li.SubItems.Add(XL_LoaiChi.Lay(p.LoaiChi).Loai)
            li.SubItems.Add(XL_TaiKhoan.Lay(p.TaiKhoan).TenTK)
            li.SubItems.Add(XL_NguoiDung.Lay(p.NguoiChi).HoTen)
            lsvphieuchi.Items.Add(li)
        Next
    End Sub

    Private Sub F8_Phieu_Chi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbx_thang.SelectedIndex = Now.Month
        Dim nam = Date.Now.Year
        Dim i As Integer = 2
        While i >= 0
            cbx_nam.Items.Add(nam - i)
            i = i - 1
        End While
        cbx_nam.SelectedItem = Date.Now.Year

        cbxloaithunhappc.DataSource = XL_LoaiChi.LayDS()
        cbxloaithunhappc.DisplayMember = "Loai"
        cbxloaithunhappc.ValueMember = "ID"

        cbxnguoitao.DataSource = XL_NguoiDung.LayDS()
        cbxnguoitao.DisplayMember = "Username"
        cbxnguoitao.ValueMember = "ID"

        cbxnguoichi.DataSource = XL_NguoiDung.LayDS()
        cbxnguoichi.DisplayMember = "Username"
        cbxnguoichi.ValueMember = "ID"

        cbxtaikhoanpc.DataSource = XL_TaiKhoan.LayDS(Me.Tag)
        cbxtaikhoanpc.DisplayMember = "TenTK"
        cbxtaikhoanpc.ValueMember = "ID"

        cbxnguoichi.SelectedValue = Me.Tag
        cbxnguoitao.SelectedValue = Me.Tag
        HienThi()
    End Sub

    Private Sub btnthempt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthempt.Click
        Dim f As New F21_Them_Phieu_Chi
        f.Tag = Me.Tag
        f.ShowDialog()
        HienThi()
    End Sub

    Private Sub btnsuapt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuapt.Click
        If lsvphieuchi.SelectedItems.Count > 0 Then
            Try
                Dim p As PhieuChi = lsvphieuchi.SelectedItems(0).Tag
                p.SoTien = Integer.Parse(txtsotienpc.Text)
                p.Ngay = dtpngaychungtupt.Value.Date
                p.LoaiChi = cbxloaithunhappc.SelectedValue
                p.NguoiChi = cbxnguoichi.SelectedValue
                p.NguoiLap = cbxnguoitao.SelectedValue
                p.TaiKhoan = cbxtaikhoanpc.SelectedValue
                p.ChiTiet = rtbchitietpc.Text
                XL_PhieuChi.CapNhat(p)
                HienThi()
            Catch ex As Exception
                Exit Sub
            End Try
        Else
            MessageBox.Show("Chọn phiếu cần cập nhật!", "Thông báo")
        End If
    End Sub

    Private Sub btnxoapt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoapt.Click
        If lsvphieuchi.SelectedItems.Count > 0 Then
            Dim p As PhieuChi = lsvphieuchi.SelectedItems(0).Tag
            XL_PhieuChi.Xoa(p)
            HienThi()
        Else
            MessageBox.Show("Chọn phiếu cần xóa!", "Thông báo")
        End If
    End Sub

    Private Sub btnthongke_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthongke.Click
        Dim f As New frTimKiemThuChi
        f.ShowDialog()
    End Sub

    Private Sub btnthoatpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoatpt.Click
        Me.Close()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim f As New F11_Loai_Chi()
        f.ShowDialog()
    End Sub

    Private Sub lsvphieuchi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvphieuchi.SelectedIndexChanged
        Try
            Dim p As PhieuChi = lsvphieuchi.SelectedItems(0).Tag
            txtsotienpc.Text = p.SoTien.ToString()
            dtpngaychungtupt.Value = p.Ngay.Date
            cbxloaithunhappc.SelectedValue = p.LoaiChi
            cbxnguoichi.SelectedValue = p.NguoiChi
            cbxnguoitao.SelectedValue = p.NguoiLap
            cbxtaikhoanpc.SelectedValue = p.TaiKhoan
            rtbchitietpc.Text = p.ChiTiet
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub Btn_hienthi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_hienthi.Click
        HienThi()
    End Sub
End Class